<?php  if (!defined("IS_INITPHP")) exit("Access Denied!");  /* INITPHP Version 1.0 ,Create on 2016-04-11 20:52:04, compiled from D:\phpStudy\WWW\Frame/app/web/template/admin/module_treelist.htm */ ?>
 
          <script src="./static/assets/js/Sortable.js"></script>

          <?php function extra_td($row,$view_fields ,$select_fields){
                foreach ($view_fields as $key =>$field){
                    if($field['notinlist'] === 0 ){
                      echo '<td class="am-text-middle" width="'.$field['width'].'">';
                        if($field['type'] === 'select'){
                            echo $select_fields[$key][$row[$key]];
                        }else{
                            echo $row[$key];
                        }
                      echo '</td>';
                    }
                }
          } ?>

          <?php function tree($tree_list,$parentid = 0 ,$table_key,$curd_url,$view_fields,$select_fields,$function_fields){
  
                echo '<ul  id="level_'.$parentid.'_w" class="admin-tree" >';  
                foreach($tree_list as $k=>$v){
                  echo '<li class="level_'.$parentid.'" >';  
                  echo  '<div class="admin-tree-line">';
                  echo '<table class="am-table"><tr>';
                  echo  '<td class="cate-td" >';
                  echo  '<input type="checkbox" name="' .$table_key .'" value="'.$v[$table_key].'"  style="display:none"/>';     
                  echo  '<span style="cursor:move" class="level_'.$parentid.'_handle">'.$v[$function_fields['tree_name_field']] .'</span>';
                  echo  '</td>';
                  extra_td($v,$view_fields ,$select_fields);
                  echo  '<td>';
                  echo  "<a href=\"$curd_url[u]&$table_key=$v[$table_key]\" class=\"am-btn  am-btn-xs \"><span class=\"am-icon-pencil-square-o\"></span></a>
                              <a  data-id=\"$v[$table_key]\" class=\"ajax-delete am-btn  am-btn-xs am-text-danger \"><span class=\"am-icon-trash-o\"></span></a>";
                  echo '</td>';
                  echo '</tr></table>';
                  echo '</div>';
                  if(isset($v['childNode'])){
                      echo '<div class="admin-tree-wrap">';
                      tree($v['childNode'],$v['id'],$table_key,$curd_url,$view_fields,$select_fields,$function_fields);
                      echo '</div>';
                   }
                }
                echo '</ul>';
                echo '<script>';
                echo  "Sortable.create(document.getElementById('level_{$parentid}_w'), {animation: 150,draggable: '.level_{$parentid}',handle: '.level_{$parentid}_handle'})";
                echo '</script>';
          } ?>
          <?php  tree($tree_list,0,$table_key,$curd_url,$view_fields,$select_fields,$function_fields) ; ?>

        <div class="am-modal am-modal-confirm" tabindex="-1" id="delete-confirm">
          <div class="am-modal-dialog">
            <div class="am-modal-hd">WXProgrammer</div>
            <div class="am-modal-bd">
              你，确定要删除这条记录吗？
            </div>
            <div class="am-modal-footer">
              <span class="am-modal-btn" data-am-modal-cancel>取消</span>
              <span class="am-modal-btn" data-am-modal-confirm>确定</span>
            </div>
          </div>
        </div>

        <script type="text/javascript">
          $(function() {

            $('#level_0_w').children('li').children('.admin-tree-line').addClass('admin-tree-level0').next('.admin-tree-wrap').children('.admin-tree').children('li').children('.admin-tree-line').addClass('admin-tree-level1').next('.admin-tree-wrap').children('.admin-tree').children('li').children('.admin-tree-line').addClass('admin-tree-level2');


            $('.ajax-delete'). on('click', function() {
                $('#delete-confirm').modal({
                  relatedTarget: this,
                  onConfirm: function(options) {
                    var $button = $(this.relatedTarget);
                    var id = $button.length ? $button.data('id') : 0;
                    $.ajax({
                        type:'post',
                        url:"<?php echo $curd_url['d'];?>",
                        data:{'<?php echo $table_key;?>':id},
                        async:false,
                        dataType:'json',
                        success:function(data){
                          if(data.status == 0){
                             alert(data.message); 
                             window.location.reload();
                          }else{
                            alert(data.message);
                          }

                        },
                        error:function(){

                        }
                     });
                  },
                  // closeOnConfirm: false,
                  onCancel: function() {
                 
                  }
                });
              });
          });
        </script>

       
        <script type="text/javascript">
            
            function order (){
                //更改排序规则
                var idwithorders = []; 
                $(":input[name='<?php echo $table_key;?>']").each(function(){
                    idwithorders.push($(this).val());
                });
                var ids  = idwithorders.join(',');
                $.ajax({
                    type:"post",
                    async:false,
                    data:{ids: ids},
                    dataType:'json',
                    url: "<?php echo $curd_url['o'];?>",
                    success:function(data){           
                       if(data.status == '0'){
                         alert(data.message);
                         location.reload();
                       }

                    }
                });
            }
        </script>
